package top.drose;

//经典例题，判断链表中是否有环（使用双指针）
//https://leetcode.cn/problems/linked-list-cycle/description/


/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
      val = x;
      next = null;
    }
}
public class P42_1 {
    public static void main(String[] args) {


    }
    //题目要求写的函数
    public boolean hasCycle(ListNode head) {
        ListNode slow,fast;
        slow =fast = head;
        while(fast != null && fast.next != null){
            fast=fast.next.next;
            slow=slow.next;
            if(slow == fast){
                return true;
            }
        }
        return false;
    }
}
